package pkg.andru.widget;

import java.util.ArrayList;
import java.util.List;

import pkg.andru.widget.BetterAdapter;
import android.content.Context;

/**
 * the drag and drop adapter, only supply for the {@link DragAndDropListView}.
 * 
 * @author idiottiger
 * 
 * @param <T>
 * @param <E>
 */
public abstract class DragAndDropAdapter<T, E> extends BetterAdapter<T, E> {

    /**
     * the adpater's items, the sub class can see this
     */
    protected List<T> mItems = new ArrayList<T>();

    private int mDragPosition = -1;

    public DragAndDropAdapter(Context context) {
        super(context);
    }

    public DragAndDropAdapter(Context context, int itemLayout) {
        super(context, itemLayout);
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public T getItem(int position) {
        return mItems.get(position);
    }

    /**
     * set the drag pos, this method just supply for the
     * {@link DragAndDropListView}
     * 
     * @param pos
     */
    void setDragPosition(int pos) {
        mDragPosition = pos;
    }

    /**
     * set the drag to where, this method just supply for
     * {@link DragAndDropListView}
     * 
     * @param moveTo
     */
    void setDragMoveToPosition(int moveTo) {
        if (moveTo >= 0 && moveTo < getCount()) {
            T item = mItems.get(mDragPosition);
            mItems.set(mDragPosition, mItems.get(moveTo));
            mItems.set(moveTo, item);
            mDragPosition = moveTo;
            notifyDataSetChanged();
        }
    }

    /**
     * return current drag position
     * 
     * @return
     */
    int getDragPosition() {
        return mDragPosition;
    }

    /**
     * check current drag to the pos
     * 
     * @param pos
     * @return
     */
    protected boolean isDragToCurrentPosition(int pos) {
        return mDragPosition == pos;
    }

}
